package autogen;

<%@page import="static hh.EmptyUtil.*" %>
<%
    String doName = toCamelCaseRemovePreAndSuffix(table.tableName, "st", "1");
    String doNameVar = lowerFist(doName);
%>


import autogen.domain.<%=doName%>DO;
import autogen.domain.<%=doName%>DTO;
import autogen.domain.<%=doName%>PageDTO;
import autogen.service.<%=doName%>Service;
import dto.Payload;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <%=table.tableName%>表 controller
 * @author mybatis-orm auto generator
 */
@RequestMapping("/root")
@RestController
@Api(tags = "xxx controller")
@Slf4j
public class <%=doName%>ApiController implements <%=doName%>Api{

    @Autowired
    <%=doName%>Service <%=doNameVar%>Service;
    
    @PostMapping("/create")
    public Payload<Boolean> create(@RequestBody @Valid <%=doName%>DTO entity) {
        return new Payload<>(<%=doNameVar%>Service.create(entity.clone(<%=doName%>DO.class)));
    }
    
    @Override
    @PostMapping("/createBatch")
    public Payload<Boolean> createBatch(@RequestBody @Valid List<<%=doName%>DTO> entities) {
        List<<%=doName%>DO> list = entities.stream().map(e -> e.clone(<%=doName%>DO.class)).collect(Collectors.toList());
        return new Payload<>(<%=doNameVar%>Service.createBatch(list));
    }
    
    @Override
    @PostMapping("/deleteById/{id}")
    public Payload<Boolean> deleteById(@PathVariable("id") Long id) {
        return new Payload<>(<%=doNameVar%>Service.deleteById(id));
    }
    
    @Override
    @PostMapping("/deleteBatchByIds")
    public Payload<Boolean> deleteBatchByIds(@RequestBody List<Long> ids) {
        return new Payload<>(<%=doNameVar%>Service.deleteBatchByIds(ids));
    }
    
    @Override
    @PostMapping("/delete")
    public Payload<Boolean> delete(@RequestBody @Valid <%=doName%>DTO entity) {
        return new Payload<>(<%=doNameVar%>Service.delete(entity.clone(<%=doName%>DO.class)));
    }
    
    @Override
    @GetMapping("/findById/{id}")
    public Payload<<%=doName%>DTO> findById(@PathVariable("id") Long id) {
        <%=doName%>DO entity = <%=doNameVar%>Service.findById(id);
        return new Payload<>(entity == null ? null : entity.clone(<%=doName%>DTO.class));
    }
    
    @Override
    @PostMapping("/listPage")
    public Payload<List<<%=doName%>DTO>> listPage(@RequestBody @Valid <%=doName%>PageDTO query) {
        List<<%=doName%>DO> entityList = <%=doNameVar%>Service.listPage(query);
        List<<%=doName%>DTO> rs = entityList.stream()
                .map(e -> e.clone(<%=doName%>DTO.class)).collect(Collectors.toList());
        return new Payload<>(rs);
    }
    
    @Override
    @PostMapping("/list")
    public Payload<List<<%=doName%>DTO>> list(@RequestBody @Valid <%=doName%>DTO entity) {
        List<<%=doName%>DTO> rs = <%=doNameVar%>Service
                .list(entity.clone(<%=doName%>DO.class)).stream()
                .map(e -> e.clone(<%=doName%>DTO.class)).collect(Collectors.toList());
        return new Payload<>(rs);
    }
    
    @Override
    @PostMapping("/findOne")
    public Payload<<%=doName%>DTO> findOne(@RequestBody @Valid <%=doName%>DTO query) {
        <%=doName%>DO entity = <%=doNameVar%>Service.findOne(query.clone(<%=doName%>DO.class));
        return new Payload<>(entity == null ? null : entity.clone(<%=doName%>DTO.class));
    }
    
    @Override
    @PostMapping("/updateById")
    public Payload<Boolean> updateById(@RequestBody @Valid <%=doName%>DTO entity) {
        return new Payload<>(<%=doNameVar%>Service.updateById(entity.clone(<%=doName%>DO.class)));
    }
    
    @Override
    @PostMapping("/updateBatchById")
    public Payload<Boolean> updateBatchByIds(@RequestBody @Valid List<<%=doName%>DTO> entities) {
        List<<%=doName%>DO> list = entities.stream().map(e -> e.clone(<%=doName%>DO.class)).collect(Collectors.toList());
        return new Payload<>(<%=doNameVar%>Service.updateBatchByIds(list));
    }

}
